package a630wj.recycleview630wj.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import a630wj.recycleview630wj.R;
import a630wj.recycleview630wj.bean.ArticleBean;

/**
 * Created by weijie on 2017/5/25.
 */

public class MyAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater LayoutInflater;
    private List<ArticleBean> list;// 保存显示的数据
    //1.自己定义构造方法
    public MyAdapter(Context context) {
        this.context=context;
        LayoutInflater= LayoutInflater.from(context);
    }
    //2.自定义获取数据
    public void setList(List<ArticleBean>list){
        this.list=list;
    }
    //3.自定义viewholder class类
    public class ViewHolder extends  RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView tv_title,tv_descript,tv_time;
        private Button button;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView=(ImageView)itemView.findViewById(R.id.imageView);
            tv_title=(TextView)itemView.findViewById(R.id.textView);
            tv_descript=(TextView)itemView.findViewById(R.id.textView2);
            tv_title=(TextView)itemView.findViewById(R.id.textView3);
            button=(Button) itemView.findViewById(R.id.button3);

        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=LayoutInflater.inflate(R.layout.item,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ArticleBean article=list.get(position);
        if(article==null)
            return;
        ViewHolder viewHolder=(ViewHolder)holder;
        viewHolder.imageView.setImageResource(article.getImgid());
        viewHolder.tv_title.setText(article.getTitle());
        viewHolder.tv_descript.setText(article.getDescription());
        //   viewHolder.tv_time.setText(article.getTime());
        viewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(position);
                notifyDataSetChanged();
            }
        });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String strid=String.valueOf(list.get(position).getArticledId());
                Toast.makeText(context,strid,Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {

        return list.size();
    }
}
